package StudyCode.summary.zipfile;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GzipWithLen {
	public static byte[] gzipWithLen(byte[] bytes) throws IOException {
		ByteArrayOutputStream baOut = null;
		GZIPOutputStream gzOut = null;
		try {
			baOut = new ByteArrayOutputStream();
//			baOut.write(EncodeUtils.int2ByteArray(bytes.length), 0, 4);
			gzOut = new GZIPOutputStream(baOut);
			gzOut.write(bytes);
			gzOut.finish();
			gzOut.flush();
			return baOut.toByteArray();
		} finally {
			closeQuietly(gzOut);
			closeQuietly(baOut);
		}
	}

	public static void closeQuietly(Closeable stream) {
		if (stream != null) {
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
